package com.gxa.modules.sys.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.AssertUtils;
import com.gxa.common.utils.PageUtils;
import com.gxa.common.utils.Result;
import com.gxa.modules.sys.entity.RoleManagement;
import com.gxa.modules.sys.entity.Staff;
import com.gxa.modules.sys.entity.StaffAdd;
import com.gxa.modules.sys.entity.StaffUpdate;
import com.gxa.modules.sys.service.RoleService;
import com.gxa.modules.sys.service.StaffService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "员工管理接口")
@RestController
@Slf4j
public class StaffController {
    @Autowired
    private StaffService staffService;

    @Autowired
    private RoleService roleService;

    @ApiOperation(value="员工管理查询(查询条件员工姓名)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value ="当前是第几页",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "limit",value ="每页显示多少条",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "order",value ="升序asc，降序填desc",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "sidx",value ="排序字段",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "employeeName",value = "员工姓名",dataType ="String"),
    })
    @GetMapping("/staff/list")
    public com.gxa.modules.community.util.Result<List<Staff>> stafflist(@RequestParam Map<String,Object> params){
        log.info("----params-----{}----",params);
        AssertUtils.isBlank(String.valueOf(params), "字符串不为能空");

        com.gxa.modules.community.util.Result<List<Staff>> r = com.gxa.modules.community.util.Result.success();
        try {
            PageInfo<Staff> pageInfo = this.staffService.queryStaff(params);
            r = com.gxa.modules.community.util.Result.success(pageInfo.getList(),pageInfo.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            r = com.gxa.modules.community.util.Result.failed();
        }
        return r;
    }

    @ApiOperation(value="员工管理 添加")
    @PostMapping("/staff/add")
    public Result<PageUtils> staffAdd(@RequestBody  StaffAdd staffAdd) {
        staffAdd.setState(1);
        log.info("----staffAdd-----{}----", staffAdd);
        AssertUtils.isBlank(String.valueOf(staffAdd), "字符串不为能空");
        String pwd = new SimpleHash("MD5", staffAdd.getPwd(), null, 1024).toString();
        staffAdd.setPwd(pwd);
        this.staffService.addStaff(staffAdd);
        return new Result<PageUtils>().ok();
    }

    @ApiOperation(value="员工管理 修改")
    @PutMapping("/staff/update")
    public Result<PageUtils> staffUpdate(@RequestBody StaffUpdate staffUpdate) {
        log.info("----staffAdd-----{}----", staffUpdate);
        AssertUtils.isBlank(String.valueOf(staffUpdate), "字符串不为能空");
        String pwd = new SimpleHash("MD5", staffUpdate.getPwd(), null, 1024).toString();
        staffUpdate.setPwd(pwd);
        this.staffService.updateStaff(staffUpdate);
        return new Result<PageUtils>().ok();
    }

    @ApiOperation(value="员工管理 删除")
    @DeleteMapping("/staff/delete")
    public Result<PageUtils> staffDalete(@RequestBody Integer[] ids) {
        log.info("----ids-----{}----", ids);
        AssertUtils.isBlank(String.valueOf(ids), "字符串不为能空");
        for (Integer id:ids){
            System.out.println(id);
            this.staffService.deleteStaff(id);
        }
        return new Result<PageUtils>().ok();
    }

    @ApiOperation(value="更改员工使用状态")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "id",value ="当前员工id",dataType ="String"),
    })
    @PutMapping("/staff/state")
    public Result<PageUtils> staffstate(@RequestParam Map<String,Object> params) {
        log.info("----params-----{}----", params);
        AssertUtils.isBlank(String.valueOf(params), "字符串不为能空");

        String id = (String) params.get("id");
        System.out.println("state--id--"+id);

        Staff staff = this.staffService.queryById(id);
        String state = staff.getState();
        System.out.println("updateberf------->"+state);
        if (state.equals("1")){
            state = "2";
        }else {
            state = "1";
        }
        System.out.println("updateafter------->"+state);
        this.staffService.updateSetState(state,id);
        return new Result<PageUtils>().ok();
    }


    @ApiOperation(value="角色对应")
    @GetMapping("/role/list")
    public Result<PageUtils> queryRole(@RequestParam Map<String,Object> params){
        log.info("----params-----{}----",params);
        AssertUtils.isBlank(String.valueOf(params), "字符串不为能空");

        PageUtils pageUtils = this.roleService.queryRole(params);
        return new Result<PageUtils>().ok(pageUtils);
    }

}
